import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { fetchValidation } from '@/utils';
import { router } from 'umi';

import { message } from 'antd';

import {
    queryUserInfo,
    queryUserList,
    queryCabinetList,
    queryUnbundleCabinet,
    queryAllCabinet,
    queryTencentDetail,
    queryRelieveImpower,
    queryGetImpower,
    queryCallbackImpower,
    queryBundleCabinet,
    queryShelfList,
    queryAllShelf
} from './service';

export type Effect = (
    action: AnyAction,
    effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
    namespace: string;
    state: {};
    effects: {
        fetchNum: Effect;
        fetchUserList: Effect;
        fetchCabinetList: Effect;
        fetchSheflList: Effect;
        fetchUnbind: Effect;
        fetchAllCabinet: Effect;
        fetchTencentDetail: Effect;
        fetchRelieveImpower: Effect;
        fetchImpower: Effect;
        fetchCallback: Effect;
        fetchBundleCabinet: Effect;
        fetchAllShelf: Effect;
    };
    reducers: {
        save: Reducer;
        clear: Reducer;
    };
}

const Model: ModelType = {
    namespace: 'tencentConfig',

    state: {
        userInfo: {},
        userList: {
            list: [],
            count: 0,
        },
        cabinetList: {
            list: [],
            count: 0,
        },
        oldShelfList: {
            list: [],
            count: 0,
        },
        newShelfList: {
            list: [],
            count: 0,
        },
        cabinetAllList: {
            list: [],
            count: 0
        },
        oldShelfAllList: {
            list: [],
            count: 0
        },
        newShelfAllList: {
            list: [],
            count: 0
        },
        tencentDetail: {},
    },

    effects: {
        *fetchNum({ payload }, { call, put }) {
            const response = yield call(queryUserInfo, payload);
            const data = fetchValidation(response);

            yield data &&
                put({
                    type: 'save',
                    payload: { userInfo: data.body },
                });
        },

        *fetchUserList({ payload }, { call, put }) {
            const response = yield call(queryUserList, payload);
            const data = fetchValidation(response);

            yield data &&
                put({
                    type: 'save',
                    payload: { userList: data.body },
                });
        },

        *fetchCabinetList({ payload }, { call, put }) {
            const response = yield call(queryCabinetList, payload);
            const data = fetchValidation(response);

            yield data &&
                put({
                    type: 'save',
                    payload: {
                        cabinetList: data.body
                            ? data.body
                            : [
                                {
                                    list: [],
                                    count: 0,
                                },
                            ],
                    },
                });
        },

        *fetchSheflList({ payload }, { call, put }) {
            const response = yield call(queryShelfList, payload);
            const data = fetchValidation(response);
            const list = payload.type === 2 ? {
                oldShelfList: data.body ? data.body : [ { list: [],  count: 0, },  ]
            } : {
                newShelfList: data.body ? data.body : [ { list: [],  count: 0, },  ]
            }
            yield data &&
                put({
                    type: 'save',
                    payload: list
                });
        },

        *fetchBundleCabinet({ payload: { cabinetIds, type, search } }, { call, put }) {
            const response = yield call(queryBundleCabinet, { cabinetIds, type });
            const data = fetchValidation(response);

            if (data) {
                message.success('绑定成功');
                if (type === 1) {
                    yield put({
                        type: 'fetchCabinetList',
                        payload: { search },
                    });
                }else {
                    yield put({
                        type: 'fetchSheflList',
                        payload: { search, type },
                    });
                }
            }
        },

        *fetchUnbind({ payload: { id, search, type } }, { call, put }) {
            const response = yield call(queryUnbundleCabinet, { id });
            const data = fetchValidation(response);

            if (data) {
                message.success('解绑成功');
                if (type === 1) {
                    yield put({
                        type: 'fetchCabinetList',
                        payload: { search },
                    });
                }else {
                    yield put({
                        type: 'fetchSheflList',
                        payload: { search, type },
                    });
                }
            }
        },

        *fetchAllCabinet({ payload }, { call, put }) {
            const response = yield call(queryAllCabinet, payload);
            const data = fetchValidation(response);

            yield data &&
                put({
                    type: 'save',
                    payload: { cabinetAllList: data.body },
                });
        },

        *fetchAllShelf({ payload }, { call, put }) {
            const response = yield call(queryAllShelf, payload);
            const data = fetchValidation(response);

            yield data &&
                put({
                    type: 'save',
                    payload: payload.type === 2 ? { oldShelfAllList: data.body } : { newShelfAllList: data.body},
                });
        },

        *fetchTencentDetail({ payload }, { call, put }) {
            const response = yield call(queryTencentDetail, payload);
            const data = fetchValidation(response);

            yield data &&
                put({
                    type: 'save',
                    payload: { tencentDetail: data.body },
                });
        },

        *fetchRelieveImpower({ payload }, { call, put }) {
            const response = yield call(queryRelieveImpower, payload);
            const data = fetchValidation(response);

            if (data) {
                message.success('解除授权成功');

                router.push({ pathname: '/tencent/impower' });
            }
        },

        *fetchImpower({ payload }, { call, put }) {
            const response = yield call(queryGetImpower, payload);
            return fetchValidation(response);
        },

        *fetchCallback({ payload }, { call, put }) {
            const response = yield call(queryCallbackImpower, payload);
            return fetchValidation(response);
        },
    },

    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
        clear(state) {
            return {
                ...state,
                cabinetAllList: {
                    list: [],
                    count: 0
                },
                oldShelfAllList: {
                    list: [],
                    count: 0
                },
                newShelfAllList: {
                    list: [],
                    count: 0
                },
            };
        }
    },
};

export default Model;
